<?


Class OrganizationModule{
    var $_connect;
    var $error;
    function OrganizationModule() {
        $this->_connect = new Dataconnection();
    }
    function getcountByCountry($country){
        $country=  str_replace(" ", "", $country);
        $SQLCommand="
        SELECT 
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null 
                AND `country`='$country') as total,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Studio%'
                AND `country`='$country') as developer,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Publisher%'
                AND `country`='$country') as publisher,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Payment%'
                AND `country`='$country') as payment
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            $result=$data->fetch_array();
            return $result;
        }
        return null;
    }
    function getcount(){
        $SQLCommand="
        SELECT 
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null
                ) as total,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Studio%'
                ) as developer,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Publisher%'
                ) as publisher,
            (SELECT count(*) FROM `organization`  
                WHERE `delete` is null AND `type` LIKE '%Payment%'
                ) as payment
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            $result=$data->fetch_array();
            return $result;
        }
        return null;
    }
    function getOrganizationByCountry($country,$groupby="''"){
        $country=  str_replace(" ", "", $country);
        $SQLCommand="
        SELECT * ,$groupby as `group`
        FROM `organization` 
        WHERE `delete` is null AND `country`='$country'
        ORDER BY  `group`
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function getGameByOr($Or){
        $SQLCommand="
        SELECT *
        FROM `game` 
        WHERE `delete` is null AND `organization`='$Or'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function getOrganizationAll($type,$groupby="''"){
        $SQLCommand="
        SELECT * ,$groupby as `group`
        FROM `organization` 
        WHERE `delete` is null ".(isset($type)?"AND `type` LIKE '%$type%'":"")."
        ORDER BY  `group`
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function getOrganizationList($_type,$p,$n,$q){
        $srt=($p-1)*$n;
        $SQLCommand="
        SELECT * 
        FROM `_organization` 
        WHERE `delete` is null AND `_type`='$_type' AND `name` LIKE '%$q%'
        ORDER BY `insert` DESC
        LIMIT $srt,$n
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function getOrganization_NumRow($q,$_type){
        
        $SQLCommand="
        SELECT count(*) AS  numrow
        FROM `_organization` 
        WHERE `delete` is null AND `_type`='$_type' AND `name` LIKE '%$q%'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        if($data!=null ){
            $row=$data->fetch_array();
            return $row["numrow"];
        }
        return 0;
    }
    function getOrganization($id){
        $SQLCommand="
        SELECT * 
        FROM `organization` 
        WHERE `delete` is null AND `id`='$id'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            $result=$data->fetch_array();
            return $result;
        }
        return null;
    }
    function insertOrganization($_type,$name,$briefname,$website,$type,$phone,$fax,$address,$country,$nation,$des){
        $acc=$_SESSION["accountID"];
        $SQLCommand="
        insert 
            `_organization`(
                `_type`,`name`,`briefname`,`website`,`type`,`phone`,
                `fax`,`address`,`country`,`nation`,`description`,`insert`,`account`
            )
            value(
                '$_type','$name','$briefname','$website','$type','$phone',
                '$fax','$address','$country','$nation','$des',now(),'$acc'
            )
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function updateOrganization($_type,$id,$name,$briefname,$website,$type,$phone,$fax,$address,$country,$nation,$des){
        $SQLCommand="
        UPDATE 
            `_organization`
            SET 
                `name`='$name',
                `briefname`='$briefname',
                `website`='$website',
                `type`='$type',
                `phone`='$phone',
                `fax`='$fax',
                `address`='$address',
                `country`='$country',
                `nation`='$nation',
                `description`='$des',
                `update`=now()
            WHERE `_type`='$_type' AND
                  `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function deleteOrganization($_type,$id){
        $SQLCommand="
        Update  
            `_organization`
            SET `delete`=now()
            WHERE `_type`='$_type' AND
                  `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
}
?>